package com.example.testmvp.presener;

import com.example.testmvp.model.LoginModelImpl;
import com.example.testmvp.model.LoginModelInterface;
import com.example.testmvp.model.User;
import com.example.testmvp.view.LoginView;

/**
 * Created by Administrator on 2017/5/23.
 */

public class LoginPresenerImpl implements LoginPresener, OnLoginFinishedListener {

    private LoginView loginView;
    private LoginModelInterface loginModel;

    public LoginPresenerImpl(LoginView loginView) {
        this.loginView = loginView;
        this.loginModel = new LoginModelImpl();
    }



    @Override
    public void validateCredentials(User user) {
        if (loginView != null) {
            loginView.showProgress();
        }

        loginModel.login(user, this);
    }

    @Override
    public void onDestroy() {
        loginView = null;
    }

    @Override
    public void onUsernameError() {
        if (loginView != null) {
            loginView.setUsernameError();
            loginView.hideProgress();
        }
    }

    @Override
    public void onPasswordError() {
        if (loginView != null) {
            loginView.setPasswordError();
            loginView.hideProgress();
        }
    }

    @Override
    public void onSuccess() {
        if (loginView != null) {
            loginView.showSuccess();
        }
    }
}
